import Vue from "vue";
import VueRouter from "vue-router";

import routes from "@/router/routers";
import { getToken } from '@/utils/token.js'

Vue.use(VueRouter);

const router = new VueRouter({
  routes,
});
const paths = ['/chat', '/user']
// 全局前置钩子
router.beforeEach((to, from, next) => {
  console.log(paths.includes(to.path))
  if (paths.includes(to.path) && !getToken()) {
    router.push('/login')
    // next('/login')
  } else {
    next()
  }
})

// 全局后置钩子
router.afterEach((to, from) => {
  // 如果当前的路由的元信息中，isRecord 的值为 true
  if (to.meta.isRecord) {
    setTimeout(() => {
      // 则把元信息中的 top 值设为滚动条纵向滚动的位置
      window.scrollTo(0, to.meta.scrollTop);
    }, 0);
  }
});

export default router;
